//
//  IAAAuthCheDetailCCell.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/7.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAAAuthCheDetailCCell.h"
#import "IAAAuthCheckModel.h"

@implementation IAAAuthCheDetailCCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.thumbnailImgView.layer.borderColor = IAA_COLOR_0xE0E0E0(1.0).CGColor;
    self.thumbnailImgView.layer.masksToBounds = YES;
//    self.thumbnailImgView.layer.borderWidth = 1;
//    self.thumbnailImgView.layer.cornerRadius = 6;
    
    [self.closeBtn addTarget:self action:@selector(closeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.cameraBtn addTarget:self action:@selector(cameraBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSelected:(BOOL)selected {
    self.thumbnailImgView.layer.borderColor = selected ? IAA_COLOR_0x129CFF(1.0).CGColor : IAA_COLOR_0xE0E0E0(1.0).CGColor;
}

- (void)closeBtnTapped:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(IAAAuthCheDetailCCell:closeBtnTapped:)]) {
        [self.delegate IAAAuthCheDetailCCell:self closeBtnTapped:button];
    }
}

- (void)cameraBtnTapped:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(IAAAuthCheDetailCCell:cameraBtnTapped:)]) {
        [self.delegate IAAAuthCheDetailCCell:self cameraBtnTapped:button];
    }
}

// 配置界面
- (void)configViewWithData:(IAAAuthCheckSortItem *)model type:(IAAAuthCheDetailCCellType)type {
    self.model = model;
    self.type = type;
    
    self.thumbnailImgView.layer.borderWidth = (type == IAAAuthCheDetailCCellTypeCircle) ? 1 : 0;
    self.thumbnailImgView.layer.cornerRadius = (type == IAAAuthCheDetailCCellTypeCircle) ? 6 : 0;
    
    self.topConstraint.constant = (type == IAAAuthCheDetailCCellTypeAllScreen) ? 0 : 12;
    self.leadingConstraint.constant = (type == IAAAuthCheDetailCCellTypeAllScreen) ? 0 : 12;
    self.trailingConstraint.constant = (type == IAAAuthCheDetailCCellTypeAllScreen) ? 0 : 12;
    self.bottomLabelHeightConstraint.constant = (type == IAAAuthCheDetailCCellTypeAllScreen) ? 0 : 17;
    
    if (model) {
        IAAAuthCheckImgItem *item = [model.imgArray firstObject];
        self.titleLabel.text = item.imageName;
        self.thumbnailImgView.image = item.imageObject;
        self.thumbnailImgView.contentMode = UIViewContentModeScaleToFill;
        if (type == IAAAuthCheDetailCCellTypeCircle) {
            self.cameraBtn.hidden = YES;
            self.closeBtn.hidden = !(item.isAdd && item.needUpload);    //上传过的就不能删除了
        } else if(type == IAAAuthCheDetailCCellTypeRect) {
            self.cameraBtn.hidden = !(item.isReTakePicture || item.isForce);
            self.closeBtn.hidden = YES;
        } else {
            self.cameraBtn.hidden = YES;
            self.closeBtn.hidden = YES;
        }
        if (item.ufid.length && !item.imageBase64.length) {
            NSString *requestStr = [IAAUrlProtocolMerge appFileImgQueryImg];
            NSDictionary *pars = @{@"ufid"   : item.ufid,
                                   @"osName" : @"iOS",
                                   @"token"  : IAA_APP_DELEGATE.token};
            [XDAFHttpRequest POST:requestStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if ([responseObject[@"resultCode"] isEqualToString:IAA_RESULT_CODE_SUC]) {
                    if (!item.imageBase64.length) {
                        item.imageBase64 = responseObject[@"imageBase64"];
                    }
                    if ([pars[@"ufid"] isEqualToString:[self.model.imgArray firstObject].ufid]) {
                        self.thumbnailImgView.image = item.imageObject;
                    }
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                ;
            }];
        }
    } else {
        self.titleLabel.text = nil;
        self.thumbnailImgView.image = [UIImage imageNamed:@"AuCh_add"];
        self.thumbnailImgView.contentMode = UIViewContentModeCenter;
        self.closeBtn.hidden = YES;
        self.cameraBtn.hidden = YES;
    }
}

@end
